package com.grape.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.crm.domain.Customer;
import com.grape.domain.base.FixedArea;
import com.grape.service.base.FixedAreaService;

@Namespace("/fixedArea")
@Scope("prototype")
@ParentPackage("json-default")
@Controller
public class FixedAreaAction extends BaseAction<FixedArea>{
	private static final long serialVersionUID = 1L;

	
	// 关联快递员到定区
	@Action(value="associationCourierToFixedArea",results=@Result(name="success",location="/pages/base/fixed_area.html",type="redirect"))
	public String associationCourierToFixedArea() {
		fas.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
	
	
	// 最简单的保存定区操作
	@Action(value="save" ,results=@Result(name="success",type="redirect",location="/pages/base/fixed_area.html"))
	public String save() {
		fas.save(model);
		
		return SUCCESS;
	}
	
	// 远程请求所有未关联定区客户
	@Action(value="a",results=@Result(name="success",type="json"))
	public String a() {
		Collection<? extends Customer> collection = WebClient.create("http://localhost:10001/crm_management/services/customerService/a")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		
		ServletActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	
	// 远程请求所有关联在定区上的客户
	@Action(value="b",results=@Result(name="success",type="json"))
	public String b() {
		Collection<? extends Customer> collection = WebClient.create("http://localhost:10001/crm_management/services/customerService/b/"+model.getId())
			.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		
		ServletActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	
	// 远程请求,关联指定客户到定区
	@Action(value="c",results=@Result(name="success",type="redirect",location="/pages/base/fixed_area.html"))
	public String c() {
		String parameter = ServletActionContext.getRequest().getParameter("customerIds");
		// System.out.println("你要的request拿多个option的value>>>>>>>>>>>>>>>"+parameter);
		
		// 当customerIds是一个null,join返回一个null,传到serviceImpl是一个"null"
		String ci = StringUtils.join(customerIds,",");
		// System.out.println("action层  >>>>>>>>客户id个数:"+customerIds.length+"       定区id:"+model.getId());
		WebClient.create("http://localhost:10001/crm_management/services/customerService/c?c1="+model.getId()+"&c2="+ci)
			.put(null);
	return SUCCESS;	
	}
	
	
	@Action(value="findAll" ,results=@Result(name="success",type="json"))
	public String findAll() {
		
		// 基本分页
		Pageable pageable = new PageRequest(page-1, rows);
		
		// 条件糖
		Specification<FixedArea> sf = new Specification<FixedArea>() {
			
			// 条件集合
			List<Predicate> lp = new ArrayList<>();
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					lp.add(p1);
				}
				

				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					lp.add(p2);
				}

				return cb.and(lp.toArray(new Predicate[0]));
			}
		};
		
		Page<FixedArea> page = fas.findAll(sf,pageable);
		
		pushPageDataToValueStack(page);
		
		return SUCCESS;
	}
	
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}


	private Integer courierId;
	private Integer takeTimeId;
	
	
	@Autowired
	private FixedAreaService fas;
	
	
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
}
